package mainSwingClass;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;

import daoClass.DogDAO;
import voClass.DogVO;


public class SwingMain extends JFrame {

	/*
	 * Swing 컴포넌트
	 */
	private JPanel contentPane;
	private JTextField txtId;
	private JPasswordField txtPwd;
	private JPanel subPane;
	private JPanel loginPane;
	private JLabel lbId; 
	private JLabel lbPwd;
	private JButton guestBtn;
	private JButton loginBtn;
	private JButton joinBtn;
	private JLabel lbImg;
	private JPanel indexPane;
	private JPanel dogDetail;
	private JPanel empDetail;
	private JPanel adoDetail;
	private JPanel mainPane;
	private JTabbedPane tabbedPane;
	private JPanel dogPane;
	private JPanel dogMain;
	private JPanel empPane;
	private JPanel empMain;
	private JPanel adoPane;
	private JPanel adoMain;
	private JPanel head;
	private JLabel lbHead;
	private CardLayout cards;
	private JLabel lbdog;
	private JLabel lbemp;
	private JLabel lbado;
	private JPanel indexMain;
	private JPanel coctentPane;
	private JPanel buttonPane;
	private JButton backBtn;
	private JPanel crudPane;
	private JPanel msgPane;
	private JPanel listPane;
	private JTable dog;
	private DogDAO ddao = new DogDAO();
	private DefaultTableModel model = new DefaultTableModel();
	private DogVO dg = new DogVO();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SwingMain frame = new SwingMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	/*
	 * 기본 생성자
	 * 추후 로그인 메소드 분리 구현을 위하여 JFrame 기능을 초기화 메소드로 별도 정의
	 */
	public SwingMain(){
		initialize();
	}
	
	
	/*
	 * Create a Frame
	 */
	private void initialize() {
		
		setTitle("유기견 관리 프로그램");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		
		/*
		 * 우측 보조 화면 판넬 정의
		 */
		subPane = new JPanel();
		subPane.setPreferredSize(new Dimension(180, 10));
		contentPane.add(subPane, BorderLayout.EAST);
		cards = new CardLayout();
		subPane.setLayout(cards);
		
		
		/*
		 * 최초 실행시 나타날 로그인 화면 및 컴포넌트 정의
		 */
		loginPane = new JPanel();
		subPane.add(loginPane, "loginPane");
		
		// ID를 입력받는 필드
		txtId = new JTextField();
		txtId.setColumns(10);
		
		// Password를 입력받는 필드
		txtPwd = new JPasswordField();
		
		// 그냥 라벨
		lbId = new JLabel("아이디");
		lbPwd = new JLabel("비밀번호");
		
		
		/*
		 * 비회원 접속 버튼 정의
		 */
		guestBtn = new JButton("비회원 접속");
		guestBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/*
				 * 로그인 기능 구현 전까지 > 비회원도 모든 기능 활성화 
				 */
				
				// 모든 탭 활성화
				tabbedPane.setEnabledAt(1, true);
				tabbedPane.setEnabledAt(2, true);
				tabbedPane.setEnabledAt(3, true);

				// 카드를 개 디테일 탭으로 전환
				cards.show(subPane, "dogDetail");
				
				// 개 탭으로 전환
				tabbedPane.setSelectedIndex(1);
				
				// 인덱스 탭 비활성화
				tabbedPane.setEnabledAt(0, false);
			}
		});
		
		
		/*
		 * 회원 로그인 버튼 정의
		 */
		loginBtn = new JButton("LOGIN");
		loginBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/*
				 * 로그인 기능 구현 전까지 > 로그인 버튼을 눌러야 탭 기능들 활성화 
				 */
				
				// 모든 탭 활성화
				tabbedPane.setEnabledAt(1, true);
				tabbedPane.setEnabledAt(2, true);
				tabbedPane.setEnabledAt(3, true);
				
				// 카드를 개 디테일 탭으로 전환
				cards.show(subPane, "dogDetail");
				
				// 개 탭으로 전환
				tabbedPane.setSelectedIndex(1);
				
				// 인덱스 탭 비활성화
				tabbedPane.setEnabledAt(0, false);
				
			}
		});
		
		
		/*
		 * 회원 가입 버튼 정의
		 * 회원 가입 폼의 별고 팝업은 지양
		 * 새로운 카드 레이어를 추가하는 방향으로
		 */
		joinBtn = new JButton("JOIN");
		
		lbImg = new JLabel("이쁜 이미지 부탁해요 ^o^");
		
		
		GroupLayout gl_loginPane = new GroupLayout(loginPane);
		gl_loginPane.setHorizontalGroup(
			gl_loginPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_loginPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_loginPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lbImg, GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
						.addGroup(gl_loginPane.createSequentialGroup()
							.addGroup(gl_loginPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(lbPwd)
								.addComponent(lbId))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_loginPane.createParallelGroup(Alignment.LEADING)
								.addComponent(txtPwd, GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
								.addComponent(txtId, GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)))
						.addGroup(gl_loginPane.createSequentialGroup()
							.addGap(28)
							.addGroup(gl_loginPane.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(joinBtn, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(guestBtn, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(loginBtn, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
					.addContainerGap())
		);
		gl_loginPane.setVerticalGroup(
			gl_loginPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_loginPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lbImg, GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
					.addGap(18)
					.addGroup(gl_loginPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lbId))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_loginPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lbPwd))
					.addGap(18)
					.addComponent(loginBtn)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(joinBtn)
					.addGap(8)
					.addComponent(guestBtn)
					.addGap(37))
		);
		loginPane.setLayout(gl_loginPane);
		
		
		/*
		 * 유기견 상세 정보 판넬 정의
		 */
		dogDetail = new JPanel();
		subPane.add(dogDetail, "dogDetail");
		dogDetail.setLayout(new BorderLayout(0, 0));
		
		lbdog = new JLabel("DOG LAYER");
		dogDetail.add(lbdog, BorderLayout.CENTER);
		
		
		/*
		 * 직원 상세 정보 판넬 정의
		 */
		empDetail = new JPanel();
		subPane.add(empDetail, "empDetail");
		empDetail.setLayout(new BorderLayout(0, 0));
		
		lbemp = new JLabel("EMP LAYER");
		empDetail.add(lbemp, BorderLayout.CENTER);
		
		
		/*
		 * 입양자 상세 정보 판넬 정의
		 */
		adoDetail = new JPanel();
		subPane.add(adoDetail, "adoDetail");
		adoDetail.setLayout(new BorderLayout(0, 0));
		
		lbado = new JLabel("ADO LAYER");
		adoDetail.add(lbado, BorderLayout.CENTER);
		
		
		/*
		 * 좌측 주요 내용들이 들어갈 탭 판넬 구역
		 */
		mainPane = new JPanel();
		mainPane.setPreferredSize(new Dimension(590, 10));
		contentPane.add(mainPane, BorderLayout.WEST);
		mainPane.setLayout(new BorderLayout(0, 0));
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBorder(null);
		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
		        String tab = "";
				switch(tabbedPane.getSelectedIndex()){
			        case 0: tab = "loginPane"; break;
			        case 1: tab = "dogDetail"; break;
			        case 2: tab = "empDetail"; break;
			        case 3: tab = "adoDetail"; break;
		        }
		        cards.show(subPane, tab);
			}
		});
		mainPane.add(tabbedPane, BorderLayout.NORTH);
		
		
		/*
		 * 최초 실행히 나타낼 탭 구역 정의
		 */
		indexPane = new JPanel();
		tabbedPane.addTab("INDEX", null, indexPane, null);
		indexPane.setLayout(new BorderLayout(0, 0));
		
		indexMain = new JPanel();
		indexPane.add(indexMain, BorderLayout.CENTER);
		
		
		/*
		 * 유기견 탭 구역
		 */
		dogPane = new JPanel();
		tabbedPane.addTab("DOG", null, dogPane, null);
		tabbedPane.setEnabledAt(1, false);
		dogMain = new JPanel();
		dogMain.setPreferredSize(new Dimension(590, 400));
		
		coctentPane = new JPanel();
		coctentPane.setPreferredSize(new Dimension(590, 310));
		
		crudPane = new JPanel();
		crudPane.setPreferredSize(new Dimension(10, 50));
		
		JButton insertBtn = new JButton("추가");
		insertBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddDog ad = new AddDog(dog);
				ad.setVisible(true);
			}
		});
		insertBtn.setPreferredSize(new Dimension(90, 29));
		
		JButton updateBtn = new JButton("수정");
		updateBtn.setPreferredSize(new Dimension(90, 29));
		
		JButton deleteBtn = new JButton("삭제");
		deleteBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int r = dog.getSelectedRow();
				if (r == -1) {
					JOptionPane.showMessageDialog(null, "삭제할 행을 선택해 주세요");
					return;
				}
				int choice = JOptionPane.showConfirmDialog(null, "정말 삭제하실건가요?");
				if (choice == JOptionPane.OK_OPTION) {
					String id = dog.getValueAt(r, 0).toString();
					if (ddao.deleteDog(id) > 0) {
						JOptionPane.showMessageDialog(null, "삭제되었습니다");
						model.removeRow(r);
						dog.repaint();
					}
				}
				
			}
		});
		deleteBtn.setPreferredSize(new Dimension(90, 29));
		GroupLayout gl_crudPane = new GroupLayout(crudPane);
		gl_crudPane.setHorizontalGroup(
			gl_crudPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_crudPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(insertBtn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 148, Short.MAX_VALUE)
					.addComponent(updateBtn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(143)
					.addComponent(deleteBtn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		gl_crudPane.setVerticalGroup(
			gl_crudPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_crudPane.createSequentialGroup()
					.addContainerGap(11, Short.MAX_VALUE)
					.addGroup(gl_crudPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(insertBtn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(deleteBtn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(updateBtn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		crudPane.setLayout(gl_crudPane);
		
		msgPane = new JPanel();
		msgPane.setPreferredSize(new Dimension(10, 30));
		msgPane.setLayout(new BorderLayout(0, 0));
		
		JLabel txtMessage = new JLabel("        상세 정보를 보시려면 강아지 번호를 더블클릭하세요\r\n");
		msgPane.add(txtMessage, BorderLayout.CENTER);
		
		listPane = new JPanel();
		GroupLayout gl_coctentPane = new GroupLayout(coctentPane);
		gl_coctentPane.setHorizontalGroup(
			gl_coctentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(crudPane, GroupLayout.PREFERRED_SIZE, 585, GroupLayout.PREFERRED_SIZE)
				.addComponent(listPane, GroupLayout.PREFERRED_SIZE, 585, GroupLayout.PREFERRED_SIZE)
				.addComponent(msgPane, GroupLayout.PREFERRED_SIZE, 585, GroupLayout.PREFERRED_SIZE)
		);
		gl_coctentPane.setVerticalGroup(
			gl_coctentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_coctentPane.createSequentialGroup()
					.addComponent(crudPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addComponent(listPane, GroupLayout.PREFERRED_SIZE, 230, GroupLayout.PREFERRED_SIZE)
					.addComponent(msgPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
		);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setPreferredSize(new Dimension(590, 220));
		listPane.add(scrollPane);
		{
			ArrayList<DogVO> listd = ddao.selectDog();
			
			model = new DefaultTableModel();
			
			model = new DefaultTableModel() {
				@Override
				public boolean isCellEditable(int row, int column) {
					return false;
				}
			};
			
			model.setColumnIdentifiers(
					new String[] { "번호", "이름", "성별", "종", "연령", "등록일", "목줄", "부서?", "발견위치", "보관부", "기타사항", "안락사여부" });
			
			for (DogVO dg : listd) {
				model.addRow(
						new String[]
								{ dg.getId(), dg.getDname(),dg.getSex(), dg.getType(), dg.getAge(), dg.getRegdate(), dg.getCollar(), dg.getFno(), dg.getSpecificloc(), dg.getStoreddept(), dg.getEtc(), dg.getMercykilling() });
				
			}
		
		
		
		dog = new JTable();
		
		dog.setModel(model);
		
		dog.setPreferredSize(new Dimension(590, 210));
		scrollPane.setViewportView(dog);
		coctentPane.setLayout(gl_coctentPane);
		}
		
		
		buttonPane = new JPanel();
		buttonPane.setPreferredSize(new Dimension(590, 50));
		
		backBtn = new JButton("돌아가기");
		GroupLayout gl_buttonPane = new GroupLayout(buttonPane);
		gl_buttonPane.setHorizontalGroup(
			gl_buttonPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_buttonPane.createSequentialGroup()
					.addContainerGap(437, Short.MAX_VALUE)
					.addComponent(backBtn)
					.addGap(43))
		);
		gl_buttonPane.setVerticalGroup(
			gl_buttonPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_buttonPane.createSequentialGroup()
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(backBtn)
					.addContainerGap())
		);
		buttonPane.setLayout(gl_buttonPane);
		GroupLayout gl_dogPane = new GroupLayout(dogPane);
		gl_dogPane.setHorizontalGroup(
			gl_dogPane.createParallelGroup(Alignment.LEADING)
				.addComponent(dogMain, GroupLayout.PREFERRED_SIZE, 585, GroupLayout.PREFERRED_SIZE)
		);
		gl_dogPane.setVerticalGroup(
			gl_dogPane.createParallelGroup(Alignment.LEADING)
				.addComponent(dogMain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
		);
		GroupLayout gl_dogMain = new GroupLayout(dogMain);
		gl_dogMain.setHorizontalGroup(
			gl_dogMain.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_dogMain.createSequentialGroup()
					.addGroup(gl_dogMain.createParallelGroup(Alignment.LEADING)
						.addComponent(coctentPane, GroupLayout.PREFERRED_SIZE, 585, GroupLayout.PREFERRED_SIZE)
						.addComponent(buttonPane, GroupLayout.PREFERRED_SIZE, 585, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		gl_dogMain.setVerticalGroup(
			gl_dogMain.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_dogMain.createSequentialGroup()
					.addComponent(coctentPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(buttonPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(22))
		);
		dogMain.setLayout(gl_dogMain);
		dogPane.setLayout(gl_dogPane);
		
		
		/*
		 * 직원 탭 구역
		 */
		empPane = new JPanel();
		tabbedPane.addTab("EMPLOYEE", null, empPane, null);
		empPane.setLayout(new BorderLayout(0, 0));
		tabbedPane.setEnabledAt(2, false);
		empMain = new JPanel();
		empPane.add(empMain, BorderLayout.CENTER);
		
		
		/*
		 * 입양자 탭 구역
		 */
		adoPane = new JPanel();
		tabbedPane.addTab("ADOPTER", null, adoPane, null);
		adoPane.setLayout(new BorderLayout(0, 0));
		tabbedPane.setEnabledAt(3, false);
		adoMain = new JPanel();
		adoPane.add(adoMain, BorderLayout.CENTER);
		
		
		/*
		 * 상위 부분 프레임
		 * 이미지를 포함한 레이블만 둘 것인지 별도의 기능을 일부 넣을 것인지 추후 결정
		 */
		head = new JPanel();
		contentPane.add(head, BorderLayout.NORTH);
		
		lbHead = new JLabel("윗부분 이미지 부탁해요~");
		head.add(lbHead);
		lbHead.setPreferredSize(new Dimension(760, 100));
	}
}
